<template>
  <el-container class="home_container">
    <el-header>
      <div class="project_info_box">
        <img src="../assets/images/avartar.png" alt />
        <span>VUE后台管理系统</span>
      </div>
      <div class="user_info_box">
        <i class="el-icon-s-custom"></i>
        <a href="javascript:;">hairui</a>
        <el-button type="info" class="home_box" @click="exit_btn">退出</el-button>
      </div>
    </el-header>
    <el-container>
      <el-aside :width="isClose ? '64px':'200px'">
        <div class="toggle_btn" @click="toggleEvent">|||</div>
        <!-- 左侧导航 -->
        <el-menu
          :default-active="activePath"
          class="el-menu-vertical-demo"
          @open="handleOpen"
          @close="handleClose"
          background-color="#333744"
          text-color="#fff"
          active-text-color="skyblue"
          :unique-opened="true"
          :collapse="isClose"
          :collapse-transition="false"
          router
        >   <!--unique-opened="true" 只允许一个菜单展开  collapse 是否折叠侧边栏 :collapse-transition 是开启动画-->
          <el-submenu index="1">   <!--v-for :index可以绑定唯一的id 字符串类型 router开启路由跳转以index 为锚点跳转-->
            <!-- template 是模板内容区域 -->
            <template slot="title">
              <i class="iconfont icon-yonghuguanli"></i>
              <span>用户管理{{activePath}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/users'" @click="getCurrentPath('/users')"><!--v-for 二级菜单 :index可以绑定唯一的id 字符串类型 -->
              <template>
                <i class="iconfont icon-houtaishangpinguanli"></i>
                <span>用户列表</span>
              </template>
            </el-menu-item>
          </el-submenu>

          <el-submenu index="2">
            <template slot="title">
              <i class="iconfont icon-quanxianguanli"></i>
              <span>权限管理</span>
            </template>
            <el-menu-item :index="'/roles'" @click="getCurrentPath('/roles')">
              <template>
                <i class="iconfont icon-houtaishangpinguanli"></i>
                <span>角色权限</span>
              </template>
            </el-menu-item>
            <el-menu-item :index="'/rights'" @click="getCurrentPath('/rights')">
              <template>
                <i class="iconfont icon-houtaishangpinguanli"></i>
                <span>列表权限</span>
              </template>
            </el-menu-item>
          </el-submenu>

          <el-submenu index="3">
            <template slot="title">
              <i class="iconfont icon-shangpinguanli"></i>
              <span>商品管理</span>
            </template>
            <el-menu-item index="/goods" @click="getCurrentPath('/goods')">
              <template>
                <i class="iconfont icon-houtaishangpinguanli"></i>
                <span>商品列表</span>
              </template>
            </el-menu-item>
            <el-menu-item index="/params" @click="getCurrentPath('/params')">
              <template>
                <i class="iconfont icon-houtaishangpinguanli"></i>
                <span>分类参数</span>
              </template>
            </el-menu-item>
            <el-menu-item index="/categories" @click="getCurrentPath('/categories')">
              <template>
                <i class="iconfont icon-houtaishangpinguanli"></i>
                <span>商品分类</span>
              </template>
            </el-menu-item>
          </el-submenu>

          <el-submenu index="4">
            <template slot="title">
              <i class="iconfont icon-dingdanguanli"></i>
              <span>订单管理</span>
            </template>
            <el-menu-item index="/orders" @click="getCurrentPath('/orders')">
              <template>
                <i class="iconfont icon-houtaishangpinguanli"></i>
                <span>订单列表</span>
              </template>
            </el-menu-item>
          </el-submenu>

          <el-submenu index="5">
            <template slot="title">
              <i class="iconfont icon-shujutongji"></i>
              <span>数据统计</span>
            </template>
            <el-menu-item index="/reports" @click="getCurrentPath('/reports')">
              <template>
                <i class="iconfont icon-houtaishangpinguanli"></i>
                <span>数据报表</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-container>
        <el-main>
            <!-- welcome 子组件 -->
            <router-view></router-view>
        </el-main>
        <el-footer>Footer</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menuList: [],
      isClose: false,
      // 当前点击激活的侧边菜单 为空默认未点击
      activePath: ''
    }
  },
  // 获取列表数据
  created() {
    // this.getMenuList()
    console.log(window.sessionStorage.getItem('activePath'))
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    exit_btn() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 点击一集菜单打开时打印 当前一集菜单的index / 数组
    handleOpen(key, keyPath) {
      console.log('打开', key, keyPath)
    },
    //点击一集菜单关闭时打印 当前一集菜单的index / 数组
    handleClose(key, keyPath) {
      console.log('关闭', key, keyPath)
    },
    //侧边栏是否关闭 默认为false 不关闭
    toggleEvent() {
      console.log(this.isClose);
      this.isClose = !this.isClose
    },
    //点击二级菜单时 触发
    getCurrentPath(currentPath) {
    // 保存当前切换的侧边子菜单
      window.sessionStorage.setItem('activePath', currentPath)
      this.activePath = currentPath
      console.log(this.activePath)
    }
    // 获取后台列表数据
    // async getMenuList() {
    //   const { data: res } = await this.$http.get('menus')
    //   if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
    //   this.menuList = res.data
    // }
  }
}
</script>
<style lang="less" scoped>
.home_container {
  height: 100%;
}
.el-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: #fff;
  background-color: #565656;
}
.project_info_box {
  display: flex;
  align-items: center;
  > img {
    width: 50px;
    border-radius: 50%;
  }
  > span {
    margin-left: 10px;
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
      border: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.user_info_box {
  a {
    margin: 0 10px;
  }
}
.el-aside .iconfont {
  margin-right: 5px;
  font-size: 22px;
}
.toggle_btn {
    cursor: pointer;
    line-height: 20px;
    letter-spacing: 1rem;
    color: #fff;
    text-align: center;
}
</style>
